<template>
    <div class="app-detail">
        <el-card class="box-card">
            <div class="container-header">
                <div class="hign-line"></div>
                <div class="header-left">
                    {{$t('inventoryManagement.baseInfo')}}
                </div>
                <el-form :model="viewModel" ref="addForm" label-width="98px" :rules="rules" class="mb8">
                    <el-row :gutter="24">
                        <el-col :span="6">
                            <el-form-item :label="$t('inventoryManagement.inspectionBatchNo')" prop="inspectionLotMark">
                                <el-input :disabled="isView" v-model="viewModel.inspectionLotMark" :placeholder="$t('public.inputPl')" clearable size="small">
                                    <template slot="prepend">EW</template>
                                </el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :span="6">
                            <el-form-item :label="$t('inventoryManagement.batch')" prop="lot">
                                <el-input :disabled="isView" v-model="viewModel.lot" :placeholder="$t('public.inputPl')" clearable size="small">
                                    <template slot="prepend">EW</template>
                                </el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :span="6">
                            <el-form-item :label="$t('inventoryManagement.supplierNo')" prop="supplierCode">
                                <el-input :disabled="isView" v-model="viewModel.supplierCode" :placeholder="$t('public.inputPl')" clearable size="small" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item :label="$t('inventoryManagement.supplierName')" prop="supplierName">
                                <el-input :disabled="isView" v-model="viewModel.supplierName" :placeholder="$t('public.inputPl')" clearable size="small" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item :label="$t('inventoryManagement.materialName')" prop="materialName">
                                <el-input :disabled="isView" v-model="viewModel.materialName" :placeholder="$t('public.inputPl')" clearable size="small" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item :label="$t('inventoryManagement.materialNo')" prop="materialCode">
                                <el-input :disabled="isView" v-model="viewModel.materialCode" :placeholder="$t('public.inputPl')" clearable size="small" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item :label="$t('inventoryManagement.incomingQuantity')" prop="batchAmount">
                                <el-input :disabled="isView" @input="(val)=>{viewModel.batchAmount = val.replace(/[^\d]/g, '')}" size="small" v-model="viewModel.batchAmount"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item :label="$t('inventoryManagement.unit')" prop="mengeneinh">
                                <el-select :disabled="isView" v-model="viewModel.mengeneinh" style="width:100%" filterable :placeholder="$t('public.select_holder',{select_holder: $t('inventoryManagement.unit')})" clearable size="small">
                                    <el-option size="mini" v-for="item in unitList" :key="item.unitName" :label="item.unitName" :value="item.unitName">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item :label="$t('inventoryManagement.factory')" prop="factoryName">
                                <el-select filterable v-model="viewModel.factoryName" transfer :placeholder="$t('public.select_holder',{select_holder: $t('inspectionManagement.plantArea')})" style="width:100%;" clearable size="small">
                                    <el-option v-for="(item) in factoryList" @click.native="changeFactory(item)" :key="item.plantName" :label="item.plantName" :value="item.plantName" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="24">
                        <el-col :span="24">
                            <el-form-item :label="$t('inventoryManagement.description')" prop="description" style="width:100%">
                                <el-input :disabled="isView" type="textarea" :autosize="{ minRows: 5, maxRows: 6}" v-model="viewModel.description" maxlength="1000">
                                </el-input>
                            </el-form-item>
                        </el-col>

                    </el-row>

                </el-form>
            </div>
        </el-card>
        <inspection-item-info ref="inspectSubmit" :itemData="inpectItemData" @callback="submitCallback"></inspection-item-info>
        <div class="submit-footer">
            <el-button icon="el-icon-close" size="small" @click="closeTab" class="drawer-close">{{$t('public.close')}}</el-button>
            <el-button v-if="!isView" type="primary" @click="submitAddForm" icon="el-icon-check" size="small">{{$t('public.saveStorage')}}</el-button>
        </div>
    </div>

</template>

<script>
import {
    listType,
    getType,
    delType,
    addType,
    updateType,
    refreshCache,
} from '@/api/basic/dict/type'
import { listALLMaterialType } from '@/api/basic/materialtype'
import { listProject } from '@/api/basic/project'
import { getNoticeList, deleteNotice, readNotice } from '@/api/system/message'
import { listAllFactory } from '@/api/basic/factory'
import { listCheckoutUnit } from '@/api/basic/checkoutUnit'
import {
    getExtraInfo,
    addExtra,
    editExtra,
} from '@/api/inventoryManagement/inspectedInventory'
import FileUploadInput from '@/components/FileUploadInput'
import MeasuringToolDialog from '@/views/components/MeasuringToolDialog'
import inspectionItemInfo from '@/views/components/InspectionItemInfo'

export default {
    name: 'inspectionExemption',
    dicts: ['sys_normal_disable'],
    components: {
        FileUploadInput,
        MeasuringToolDialog,
        inspectionItemInfo,
    },
    data() {
        return {
            direction: 'rtl',
            addInspectionExemption: false,
            factoryList: [],
            code: 'EW',
            id: this.$route.params.id || null,
            drawer: false,
            approvalOpen: false,
            viewInspectionExemption: false,
            extraList: {
                samplingOne: null,
                samplingTwo: null,
                samplingThree: null,
                measuringTool: null,
                inspectionType: '1',
                specialCharacteristics: '0',
                projectCode: null,
                itemFrequency: '批',
                itemFrequencyValue: null,
            },
            formTable: {
                extraList: [],
            },
            projectList: [],
            addModel: {},
            viewModel: {},
            editModel: {},
            approvalForm: {
                status: '1',
                textarea: '',
            },
            isView: this.$route.query.isView ? true : false,
            unitList: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                noticeTitle: undefined,
                noticeType: undefined,
                hasRead: undefined,
                createTime: undefined,
            },
            // 表单参数
            form: {},
            materialOptions: [],
            // 表单校验
            rules: {
                factoryName: [
                    {
                        required: true,
                        message: this.$t('formValidation.factoryAreaCannotBeEmpty'),
                        trigger: 'blur',
                    },
                ],
                materialName: [
                    {
                        required: true,
                        message: this.$t('formValidation.materialNameCannotBeEmpty'),
                        trigger: 'blur',
                    },
                ],
                materialCode: [
                    {
                        required: true,
                        message: '物料编号不能为空',
                        trigger: 'blur',
                    },
                ],
                materialType: [
                    {
                        required: true,
                        message: '物料类型不能为空',
                        trigger: 'change',
                    },
                ],
                supplierCode: [
                    {
                        required: true,
                        message: '供应商编号不能为空',
                        trigger: 'blur',
                    },
                ],
                supplierName: [
                    {
                        required: true,
                        message: '供应商名称不能为空',
                        trigger: 'blur',
                    },
                ],
                inspectionLotMark: [
                    {
                        required: true,
                        message: '批次号不能为空',
                        trigger: 'blur',
                    },
                ],
                lot: [
                    {
                        required: true,
                        message: '批次不能为空',
                        trigger: 'change',
                    },
                ],
                batchAmount: [
                    {
                        required: true,
                        message: '来料数量不能为空',
                        trigger: ['blur', 'change'],
                    },
                ],
                mengeneinh: [
                    {
                        required: true,
                        message: '请选择单位',
                        trigger: 'change',
                    },
                ],
                maturity: [
                    {
                        required: true,
                        message: '成熟度不能为空',
                        trigger: 'blur',
                    },
                ],
            },
            rulesForm: {},
            fullscreen: false,
            inpectItemData: [],
        }
    },
    created() {
        this.getProjectList()
        this.getMaterialType()
        this.getFactoryList()
        this.getUnitList()
        if (this.id != '0') {
            this.getExtraInfo()
        } else {
            this.inpectItemData = []
        }
    },
    methods: {
        getFactoryList() {
            listAllFactory().then((res) => {
                this.factoryList = res.data
            })
        },
        changeFactory(dict) {
            this.viewModel.factoryName = dict.plantName
            this.viewModel.factoryCode = dict.plantCode
        },
        getExtraInfo() {
            this.loading = true
            getExtraInfo(this.id).then((response) => {
                this.loading = false
                if (response.code == 200 && response.data) {
                    let data = response.data
                    data.inspectionLotMark = data.inspectionLotMark.substr(2)
                    data.lot = data.lot.substr(2)
                    this.viewModel = data
                    this.inpectItemData = data.checkItemList
                }
            })
        },
        getUnitList() {
            listCheckoutUnit().then((res) => {
                this.unitList = res.rows
            })
        },
        getProjectList() {
            listProject().then((res) => {
                this.projectList = res.rows
            })
        },
        getMaterialType() {
            // listALLMaterialType().then((res) => {
            //     this.materialOptions = res.data
            // })
        },
        closeTab() {
            this.$router.go(-1)
        },
        /** 提交按钮 */
        submitForm: function () {
            let isTrue = false
            this.$refs['addForm'].validate((valid) => {
                if (valid) {
                    isTrue = true
                } else {
                    this.$modal.msgError(this.$t('formValidation.theBasicInformationIsIncomplete'))
                    isTrue = false
                }
            })
            return isTrue
        },

        /** 发起检验提交 */
        submitAddForm: function () {
            if (this.submitForm()) {
                this.$refs.inspectSubmit.submitAddForm()
            }
        },
        submitCallback(params) {
            let submitData = {
                ...this.viewModel,
                checkItemList: params,
            }
            submitData.inspectionLotMark = 'EW' + submitData.inspectionLotMark
            submitData.lot = 'EW' + submitData.lot
            let action = this.id == '0' ? addExtra : editExtra
            action(submitData).then((response) => {
                this.$modal.msgSuccess('提交成功!')
                this.$router.go(-1)
            })
        },
    },
}
</script>
<style lang="scss" scoped>
::v-deep .el-card {
    padding-top: 20px;
    border: 0px;
    box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.08);
    border-radius: 4px;
    padding-bottom: 20px;
}
::v-deep .el-aside {
    padding: 0;
    margin-bottom: 0;
}
.content-style {
    border: 0px;
    box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.08);
    border-radius: 2px;
    background-color: white;
    margin-top: 10px;
}
::v-deep .el-button-group .selBtn {
    background: #fff;
    border: 1px solid #0b7be3;
    color: #0b7be3;
}
::v-deep .el-button-group .unselBtn {
    background: #fff;
    border: 1px solid #ededf2;
    color: #666666;
}
.header-left {
    float: left;
    font-size: 14px;
    font-family: PingFangSC-Semibold, PingFang SC;
    font-weight: 600;
    color: #333333;
    line-height: 40px;
    display: flex;
    justify-content: center;
}
.header-right {
    float: right;
}
.container-header {
    float: left;
    width: 100%;
    border-bottom: 1px solid #ededf2;
    padding-left: 12px;
}
.container-table {
    float: left;
    margin-top: 6px;
    width: 100%;
    // max-height: calc(100vh - 600px);
    overflow: auto;
}
.mb8 {
    float: left;
    margin-top: 6px;
    width: 100%;
}
.drawer-footer {
    display: flex;
    position: relative;
    z-index: 9999;
    bottom: -40px;
    height: 40px;
    justify-content: center;
    width: 100%;
}
.hign-line {
    position: relative;
    left: -8px;
    top: 14px;
    height: 12px;
    border-radius: 2px;
    border: 1px solid #0b7be3;
    float: left;
}
.drawer-close {
    background: #f7f8fa;
    border-color: #f7f8fa;
}
.submit-footer {
    height: 52px;
    background: #ffffff;
    box-shadow: 0px -5px 5px 0px rgba(0, 0, 0, 0.08);
    border-radius: 4px 4px 0px 0px;
    position: fixed;
    z-index: 9;
    bottom: 0px;
    width: 100%;
    left: 0px;
    display: flex;
    justify-content: center;
    align-items: center;
}
.samplingPercent {
    position: absolute;
    z-index: 9;
    right: 20px;
}
::v-deep .el-card {
    padding-top: 0px;
}
.app-detail {
    max-height: calc(100vh - 180px);
    overflow: auto;
}
::v-deep .el-table .el-form-item {
    margin-bottom: 0;
}
::v-deep .el-form-item {
    margin-bottom: 3px !important;
}
::v-deep .el-input-group {
    vertical-align: baseline;
}
::v-deep .multipleInput .cell {
    display: flex;
}
</style>
